

#Load Packages 

library(ggplot2)
library(haven)
library(gridExtra)
library(dataverse)

# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")

###
# Stauffer
###

state.prob <- get_dataframe_by_name(
  filename = "figure_3.tab",
  dataset = "10.7910/DVN/QYZGEI", 
  server = "dataverse.harvard.edu")


labels <- c(a = "Not At All", b = "Not Very", c = "Moderately", d = "Very")
state.plot<-ggplot(state.prob, aes(estimate, y=prob, ))+facet_grid(.~outcome, labeller=labeller(outcome=labels))+
  geom_line(size=2) +
  geom_ribbon(size=1.5, alpha=.3, colour=NA, aes(ymin=lower, ymax=upper))+ scale_colour_manual(values=c("forestgreen", "dodgerblue2", "darkorchid4", "darkgoldenrod2"), name="Responsive", labels=c("Very", "Moderately", "Not Very", "Not at All"))+
  scale_fill_manual(values=c("forestgreen", "dodgerblue2", "darkorchid4", "darkgoldenrod2"), name="Responsive", labels=c("Very", "Moderately", "Not Very", "Not at All"))+
  ylab("Predicted Probability \n")+
  xlab("Estimate % Women \n ")+
  ggtitle(" ")+
  theme_bw()+
  xlab("")
state.plot<-state.plot+  xlab(" \n Estimate % Women")
state.plot<-state.plot+theme_bw() + theme(panel.grid.major = element_blank(),
                                          panel.grid.minor = element_blank())
state.plot<-state.plot+  theme(axis.text=element_text(size=20),
                               axis.title=element_text(size=20, face="bold"),
                               plot.title=element_text(size=22, face="bold"), panel.spacing = unit(0.75, "lines"))
state.plot<-state.plot+coord_cartesian(ylim=c(0, .6))
state.plot<-state.plot+scale_y_continuous(breaks=c(.1, .2, .3, .4, .5, .6))
state.plot<-state.plot+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits=c(0,50))+
  theme(legend.justification=c(.5,.95), legend.position=c(.075,.95), legend.text=element_text(size=12), 
        legend.title=element_text(size=14))
state.plot<-state.plot+theme(legend.key.width=unit(1.5,"cm"))
state.plot<-state.plot+theme(axis.line.x=element_line(color="black", size=.5), axis.line.y=element_line(color="black", size=.5))+
  theme(strip.text.x = element_text(size = 16))

tiff(filename = "Figures/Appendix/Figure-A-5-Panel-A.tiff",
     width = 12, height = 4, units = "in", pointsize = 12, res=1200)

state.plot
dev.off()


###
# Clean Up STATA Output
###


CleanUpSTATA <- function(StataEstimates)
{
  # Clean up the STATA Output
  StataEstimates$Remove <- substr(StataEstimates$X.,1, 10)
  StataEstimates <- subset(StataEstimates, StataEstimates$Remove=="=_missing_")
  StataEstimates$X. <- gsub("=_missing_",".", StataEstimates$X.)
  
  congress <- data.frame(matrix(NA, nrow(StataEstimates),0))
  congress$pooledestimate <- as.numeric(as.character(StataEstimates$X.))
  return(congress)
}

CreateStaufferFormat <- function(outcome, Margins, Lower, Upper)
{
  pooledmargin <- CleanUpSTATA(Margins)
  pooledul <- CleanUpSTATA(Upper)
  pooledll <- CleanUpSTATA(Lower)
  congress <- as.data.frame(cbind(outcome, pooledmargin,pooledul, pooledll, seq(0,60,5)))
  colnames(congress) <- c("outcome", "prob", "upper", "lower", "estimate")
  return(congress)
}

###
# Gender
###

    OutcomeFileNames <- c("Neg2", "Neg1","Zero","Pos1", "Pos2")
    Outcomes <- c("a", "b", "c", "d", "e")
    
    for(i in 1:5)
      {
      ClarkRogersEstimates <- read.csv(paste("Scripts/Figures/Estimates/ClarkRogers-FigureA-5-Gender-Outcome-",OutcomeFileNames[i],"-Margins.csv", sep=""))
      ClarkRogersUpper <- read.csv(paste("Scripts/Figures/Estimates/ClarkRogers-FigureA-5-Gender-Outcome-",OutcomeFileNames[i],"-lowerbound.csv", sep=""))
      ClarkRogersLower <- read.csv(paste("Scripts/Figures/Estimates/ClarkRogers-FigureA-5-Gender-Outcome-",OutcomeFileNames[i],"-upperbound.csv", sep=""))
      CurrentEstimates <- CreateStaufferFormat(Outcomes[i],ClarkRogersEstimates, ClarkRogersUpper, ClarkRogersLower)
        
      if(i==1){Estimates <- CurrentEstimates}
      if(i>1){Estimates <- rbind(Estimates,CurrentEstimates)}
      }
  
    state.prob <- Estimates
    #state.prob <- read.csv("Dropbox/Clark-Rogers/Descriptive Representation/Stauffer Replication/Scripts/Figures/Estimates/Rogers-Figure-3-Gender-Estimates.csv")
    
    labels <- c(a = "Strongly Disagree", b = "Somewhat Disagree", c = "Neither", d = "Somewhat Agree", e = "Strongly Agree")
    state.plot<-ggplot(state.prob, aes(estimate, y=prob, ))+facet_grid(.~outcome, labeller=labeller(outcome=labels))+
      geom_line(size=2) +
      geom_ribbon(size=1.5, alpha=.3, colour=NA, aes(ymin=lower, ymax=upper))+ scale_colour_manual(values=c("forestgreen", "dodgerblue2", "darkorchid4", "darkgoldenrod2"), name="Responsive", labels=c("Very", "Moderately", "Not Very", "Not at All"))+
      scale_fill_manual(values=c("forestgreen", "dodgerblue2", "darkorchid4", "darkgoldenrod2"), name="Responsive", labels=c("Very", "Moderately", "Not Very", "Not at All"))+
      ylab("Predicted Probability \n")+
      xlab("Estimate % Women \n ")+
      ggtitle(" ")+
      theme_bw()+
      xlab("")
    state.plot<-state.plot+  xlab(" \n Estimate % Women")
    state.plot<-state.plot+theme_bw() + theme(panel.grid.major = element_blank(),
                                              panel.grid.minor = element_blank())
    state.plot<-state.plot+  theme(axis.text=element_text(size=20),
                                   axis.title=element_text(size=20, face="bold"),
                                   plot.title=element_text(size=22, face="bold"), panel.spacing = unit(0.75, "lines"))
    state.plot<-state.plot+coord_cartesian(ylim=c(0, .6))
    state.plot<-state.plot+scale_y_continuous(breaks=c(.1, .2, .3, .4, .5, .6))
    state.plot<-state.plot+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits=c(0,50))+
      theme(legend.justification=c(.5,.95), legend.position=c(.075,.95), legend.text=element_text(size=12), 
            legend.title=element_text(size=14))
    state.plot<-state.plot+theme(legend.key.width=unit(1.5,"cm"))
    state.plot<-state.plot+theme(axis.line.x=element_line(color="black", size=.5), axis.line.y=element_line(color="black", size=.5))+
      theme(strip.text.x = element_text(size = 16))
    
    tiff(filename = "Figures/Appendix/Figure-A-5-Panel-B.tiff",
         width = 12, height = 4, units = "in", pointsize = 12, res=1200)
        
      state.plot
    dev.off()


  ###
  # Race
  ###

    OutcomeFileNames <- c("Neg2", "Neg1","Zero","Pos1", "Pos2")
    Outcomes <- c("a", "b", "c", "d", "e")
    
    for(i in 1:5)
    {
      ClarkRogersEstimates <- read.csv(paste("Scripts/Figures/Estimates/ClarkRogers-FigureA-5-Race-Outcome-",OutcomeFileNames[i],"-Margins.csv", sep=""))
      ClarkRogersUpper <- read.csv(paste("Scripts/Figures/Estimates/ClarkRogers-FigureA-5-Race-Outcome-",OutcomeFileNames[i],"-lowerbound.csv", sep=""))
      ClarkRogersLower <- read.csv(paste("Scripts/Figures/Estimates/ClarkRogers-FigureA-5-Race-Outcome-",OutcomeFileNames[i],"-upperbound.csv", sep=""))
      CurrentEstimates <- CreateStaufferFormat(Outcomes[i],ClarkRogersEstimates, ClarkRogersUpper, ClarkRogersLower)
      
      if(i==1){Estimates <- CurrentEstimates}
      if(i>1){Estimates <- rbind(Estimates,CurrentEstimates)}
    }
    
    state.prob <- Estimates
    
    labels <- c(a = "Strongly Disagree", b = "Somewhat Disagree", c = "Neither", d = "Somewhat Agree", e = "Strongly Agree")
    state.plot<-ggplot(state.prob, aes(estimate, y=prob, ))+facet_grid(.~outcome, labeller=labeller(outcome=labels))+
      geom_line(size=2) +
      geom_ribbon(size=1.5, alpha=.3, colour=NA, aes(ymin=lower, ymax=upper))+ scale_colour_manual(values=c("forestgreen", "dodgerblue2", "darkorchid4", "darkgoldenrod2"), name="Responsive", labels=c("Very", "Moderately", "Not Very", "Not at All"))+
      scale_fill_manual(values=c("forestgreen", "dodgerblue2", "darkorchid4", "darkgoldenrod2"), name="Responsive", labels=c("Very", "Moderately", "Not Very", "Not at All"))+
      ylab("Predicted Probability \n")+
      xlab("Estimate % Black \n ")+
      ggtitle(" ")+
      theme_bw()+
      xlab("")
    state.plot<-state.plot+  xlab(" \n Estimate % Blacks")
    state.plot<-state.plot+theme_bw() + theme(panel.grid.major = element_blank(),
                                              panel.grid.minor = element_blank())
    state.plot<-state.plot+  theme(axis.text=element_text(size=20),
                                   axis.title=element_text(size=20, face="bold"),
                                   plot.title=element_text(size=22, face="bold"), panel.spacing = unit(0.75, "lines"))
    state.plot<-state.plot+coord_cartesian(ylim=c(0, .6))
    state.plot<-state.plot+scale_y_continuous(breaks=c(.1, .2, .3, .4, .5, .6))
    state.plot<-state.plot+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits=c(0,50))+
      theme(legend.justification=c(.5,.95), legend.position=c(.075,.95), legend.text=element_text(size=12), 
            legend.title=element_text(size=14))
    state.plot<-state.plot+theme(legend.key.width=unit(1.5,"cm"))
    state.plot<-state.plot+theme(axis.line.x=element_line(color="black", size=.5), axis.line.y=element_line(color="black", size=.5))+
      theme(strip.text.x = element_text(size = 16))
    
    tiff(filename = "Figures/Appendix/Figure-A-5-Panel-C.tiff",
         width = 12, height = 4, units = "in", pointsize = 12, res=1200)
    
    state.plot
    dev.off()
    
